package com.kangkangqing.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kangkangqing.constants.SystemConstants;
import com.kangkangqing.domain.entity.Article;
import com.kangkangqing.domain.entity.ResponseResult;
import com.kangkangqing.domain.vo.HotArticleVo;
import com.kangkangqing.mapper.ArticleMapper;
import com.kangkangqing.service.ArticleService;
import com.kangkangqing.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {



    //查询热门文章
    //浏览量前10，展示标题和浏览量,可点击详情
    //不展示草稿和删除
    @Override
    public ResponseResult<List> getHotArticleList() {
        //创建查询语句
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //设置排序
        queryWrapper.orderByDesc(Article::getViewCount);

        //设置分页规则
        Page<Article> page = new Page<>(1,10);
        //传入分页规则
        page(page,queryWrapper);
        //获取分页结果
        List<Article> records = page.getRecords();

        //数据脱敏
//        List<HotArticleVo> list=new ArrayList<>();
//        for (Article article : records) {
//            HotArticleVo hotArticleVo = new HotArticleVo();
//            BeanUtils.copyProperties(article,hotArticleVo);
//            list.add(hotArticleVo);
//        }
        List<HotArticleVo> list = BeanCopyUtils.beanCopyList(records, HotArticleVo.class);
        return ResponseResult.okResult(list);
    }


}
